package com.xiaoxing.stepcircle;

import android.animation.ValueAnimator;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private MyCountDownProgress countDownProgress;
    private int progress;

    Button btnPause;
    Button btnStop;
    Button btnPlay;
    ValueAnimator animator;

    static final int ID_BTN_PLAY = 1001, ID_BTN_PAUSE = 1002, ID_BTN_STOP = 1003;
    MyAnimatorUpdateListener updateListener = new MyAnimatorUpdateListener();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnPlay = (Button) findViewById(R.id.btn_kaishi);
        btnPause = (Button) findViewById(R.id.btn_zanting);
        btnStop = (Button) findViewById(R.id.btn_tingzhi);
        btnPlay.setOnClickListener(this);
        btnPause.setOnClickListener(this);
        btnStop.setOnClickListener(this);
        countDownProgress = (MyCountDownProgress) findViewById(R.id.countdownProgress);
//        countDownProgress.setCountdownTime(1);
//        countDownProgress.setTimerTask();
//        countDownProgress.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                countDownProgress.startCountDownTime(new MyCountDownProgress.OnCountdownFinishListener() {
//                    @Override
//                    public void countdownFinished() {
//                        Toast.makeText(MainActivity.this, "倒计时结束了--->该UI处理界面逻辑了", Toast.LENGTH_LONG).show();
//                    }
//                });
//                /*Message message = Message.obtain();
//                message.what = HANDLER_MESSAGE;
//                handler.sendMessage(message);*/
//            }
//        });

        animator = ValueAnimator.ofFloat(0, 1.0f);
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.setDuration(1000 * 60);
        //为了增加
        animator.addUpdateListener(updateListener);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.btn_kaishi:

                isPause = false;
                startTimer();

                //如果已经暂停，是继续播放
                if (updateListener.isPause) updateListener.play();
                    //否则就是从头开始播放
                else animator.start();


                break;
            case R.id.btn_tingzhi:

                isPause = true;
                stopTimer();

                //如果点击停止，那么我们还需要将暂停的动画重新设置一下
                updateListener.play();
                animator.end();

                break;
            case R.id.btn_zanting:
                updateListener.pause();

                isPause = true;

                break;
        }

    }

    class MyAnimatorUpdateListener implements ValueAnimator.AnimatorUpdateListener {
        /**
         * 暂停状态
         */
        private boolean isPause = false;
        /**
         * 是否已经暂停，如果一已经暂停，那么就不需要再次设置停止的一些事件和监听器了
         */
        private boolean isPaused = false;
        /**
         * 当前的动画的播放位置
         */
        private float fraction = 0.0f;
        /**
         * 当前动画的播放运行时间
         */
        private long mCurrentPlayTime = 0l;

        /**
         * 是否是暂停状态
         *
         * @return
         */
        public boolean isPause() {
            return isPause;
        }

        /**
         * 停止方法，只是设置标志位，剩余的工作会根据状态位置在onAnimationUpdate进行操作
         */
        public void pause() {
            isPause = true;
        }

        public void play() {
            isPause = false;
            isPaused = false;
        }

        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            /**
             * 如果是暂停则将状态保持下来，并每个刷新动画的时间了；来设置当前时间，让动画
             * 在时间上处于暂停状态，同时要设置一个静止的时间加速器，来保证动画不会抖动
             */
            if (isPause) {
//                if (!isPaused) {
//                    mCurrentPlayTime = animation.getCurrentPlayTime();
//                    fraction = animation.getAnimatedFraction();
//                    animation.setInterpolator(new TimeInterpolator() {
//                        @Override
//                        public float getInterpolation(float input) {
//                            return fraction;
//                        }
//                    });
//                    isPaused = true;
//                }
                //每隔动画播放的时间，我们都会将播放时间往回调整，以便重新播放的时候接着使用这个时间,同时也为了让整个动画不结束
                new CountDownTimer(ValueAnimator.getFrameDelay(), ValueAnimator.getFrameDelay()) {

                    @Override
                    public void onTick(long millisUntilFinished) {
                    }

                    @Override
                    public void onFinish() {
                        animator.setCurrentPlayTime(mCurrentPlayTime);
                    }
                }.start();
            } else {
                //将时间拦截器恢复成线性的，如果您有自己的，也可以在这里进行恢复
                animation.setInterpolator(null);

                countDownProgress.setCurrentAngle((float) animation.getAnimatedValue());
                countDownProgress.setTextDesc(String.valueOf(count));
                //Log.e("currentAngle",currentAngle+"");
                //实时刷新view，这样我们的进度条弧度就动起来了
                countDownProgress.initValue();
            }
        }

    }


    private Timer mTimer = null;
    private TimerTask mTimerTask = null;

    private Handler mHandler = null;

    private static int count = 0;
    private boolean isPause = true;
    private boolean isStop = false;

    private static int delay = 0;  //1s
    private static int period = 1000;  //1s
    private static final int UPDATE_TEXTVIEW = 0;

    private void startTimer() {

        if (mTimerTask == null) {
            mTimerTask = new TimerTask() {
                @Override
                public void run() {
                    sendMessage(UPDATE_TEXTVIEW);

                    do {
//                        try {
//                            Thread.sleep(1000);
//                        } catch (InterruptedException e) {
//                        }
                    } while (isPause);

                    count++;
                }
            };
        }

        if (mTimer == null) {
            mTimer = new Timer();
            if (mTimerTask != null) {
                mTimer.schedule(mTimerTask, delay, period);
            }
        }


    }

    private void stopTimer() {

        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
        }

        if (mTimerTask != null) {
            mTimerTask.cancel();
            mTimerTask = null;
        }

        count = 0;

    }

    public void sendMessage(int id) {
        if (mHandler != null) {
            Message message = Message.obtain(mHandler, id);
            mHandler.sendMessage(message);
        }
    }


}
